﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ENG.TeachersDiary.Model;
using ENG.TeachersDiary.Classes;

namespace ENG.TeachersDiary
{
  public partial class FrmInsertSetOfMarks : Form
  {
    DiaryDB db;
    Subject currentSubject;
    Class currentClass;

    public FrmInsertSetOfMarks()
    {
      InitializeComponent();

      ReDB();
    }

    public void ReDB ()
    {
      db = Common.DBConn;

      currentSubject = Common.GetCurrentSubject(db);
      currentClass = Common.GetCurrentClass(db);

      markGroupBindingSource.DataSource = null;
      markGroupBindingSource.DataSource =
        currentSubject.MarkGroups.Where(i => i.IsToOffer).OrderBy(i => i.OrderIndex);

    }

    internal void Init(int? markGroupID)
    {
      if (markGroupID.HasValue)
      {
        cmbMarkGroup.SelectedItem =
          currentSubject.MarkGroups.First(i => i.MarkGroupID == markGroupID);
      }
    }

    private void FrmInsertSetOfMarks_Load(object sender, EventArgs e)
    {      

      var students =
        currentClass.Students.OrderBy(i => i.SurnameName);

      CtrStudentMark csm = null;

      int top = 0;
      bool isOdd = false;
      int width = plnFlow.Width;
      System.Drawing.Color altColor = System.Drawing.Color.FromArgb(255, 255, 192);

      foreach (var fItem in students)
      {
        csm = new CtrStudentMark();
        plnFlow.Controls.Add(csm);

        csm.Student = fItem;
        csm.Width = width;
        csm.Left = 0;
        csm.Top = top;
        csm.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
        top += csm.Height;
        if (isOdd)
          csm.BackColor = System.Drawing.Color.White;
        else
          csm.BackColor = altColor;
        isOdd = !isOdd;
      } // foreach (var fItem in students)

    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
      this.Close();
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
      changedStudentID = new DistinctList<int>() { RaiseExceptionOnDistinctInsert = false };

      Model.Mark m = null;
      Model.MarkGroup mg = cmbMarkGroup.SelectedItem as Model.MarkGroup;
      DateTime date = dtpDate.Value;
      Model.Student s = null;
      Model.MarkCode mc = null;
      CtrStudentMark ctr = null;
      var col = db.Marks;

      foreach (var fItem in plnFlow.Controls)
      {
        ctr = fItem as CtrStudentMark;
        s = ctr.Student;
        mc = ctr.MarkCode;

        if (!mc.IsEvaluable) // pokud nevybrano, jdeme dal
          continue;

        m = new Model.Mark();

        m.Date = date;
        m.MarkCodeID = mc.MarkCodeID;
        m.MarkGroupID = mg.MarkGroupID;
        m.StudentID = s.StudentID;
        m.Note = txtNote.Text;

        col.InsertOnSubmit(m);

        changedStudentID.Add(s.StudentID);
      } // foreach (var fItem in collection)

      
      db.SubmitChanges();

      this.Close();
    }

    DistinctList<int> changedStudentID = null;
    public DistinctList<int> GetChangedStudentsIDs()
    {
      return changedStudentID;
    }

    private void btnCancel_Click_1(object sender, EventArgs e)
    {
      this.changedStudentID = null;
    }
  }
}
